local skel = fk.CreateSkill {
  name = "lb__lizhu",
  tags = { Skill.Switch },
}

Fk:loadTranslationTable {
  ["lb__lizhu"] = "理逐",
  [":lb__lizhu"] = "<b>转换技</b>，出牌阶段，你可以将任意张手牌置于牌堆：阳：顶；阴：底；然后从另一端摸至多等量牌并展示之，依次视为使用前X张牌中本回合未以此法使用过的基本牌或普通锦囊牌（X为你少摸的牌数）。若X小于1，本回合此技能失效。",

  [":lb__lizhu_yang"] = "<b>转换技</b>，出牌阶段，你可以将任意张手牌置于牌堆：<font color=\"#E0DB2F\">阳：顶；</font>阴：底；然后从另一端摸至多等量牌并展示之，依次视为使用前X张牌中本回合未以此法使用过的基本牌或普通锦囊牌（X为你少摸的牌数）。若X小于1，本回合此技能失效。",
  [":lb__lizhu_yin"] = "<b>转换技</b>，出牌阶段，你可以将任意张手牌置于牌堆：阳：顶；<font color=\"#E0DB2F\">阴：底；</font>然后从另一端摸至多等量牌并展示之，依次视为使用前X张牌中本回合未以此法使用过的基本牌或普通锦囊牌（X为你少摸的牌数）。若X小于1，本回合此技能失效。",


  ["#lb__lizhu"] = "理逐：你可以将任意张手牌置于%arg（顺序为左侧在上，与〖观星〗相同）",
  ["#lb__lizhu-ask"] = "理逐：你可以从%arg摸任意张牌",
  ["@$lb__lizhu-turn"] = "理逐",

  ["$lb__lizhu1"] = "存在，即被感知。",
  ["$lb__lizhu2"] = "定将穷尽真理，根除谬误。",
  ["$lb__lizhu3"] = "知识即为万物尺度。",
}

skel:addEffect("active", {
  anim_type = "switch",
  dynamic_desc = function (self, player, lang)
    return player:getSwitchSkillState(skel.name) == fk.SwitchYang and ":lb__lizhu_yang" or ":lb__lizhu_yin"
  end,
  prompt = function (self, player, selected_cards, selected_targets)
     return "#lb__lizhu:::" .. (player:getSwitchSkillState(skel.name) == fk.SwitchYang and "Top" or "Bottom")
  end,
  can_use = function(self, player)
    return not player:isKongcheng() --and player:getMark("lb__lizhu-turn") == 0
  end,
  target_filter = Util.FalseFunc,
  card_filter = function(self, player, to_select, selected)
    return table.contains(player:getCardIds("h"), to_select)
  end,
  min_card_num = 1,
  on_use = function(self, room, effect)
    local player = effect.from
    local ids = {}
    for _, id in ipairs(player:getCardIds("h")) do
      if table.contains(effect.cards, id) then
        table.insert(ids, id)
      end
    end
    local move = {
      ids = table.reverse(ids),
      from = player.id,
      toArea = Card.DrawPile,
      moveReason = fk.ReasonPut,
      --drawPilePosition = -1,
      skillName = skel.name,
      proposer = player,
      moveVisible = true,
    }
    if player:getSwitchSkillState(skel.name) == fk.SwitchYang then
      move.ids = ids
      move.drawPilePosition = -1
    end
    room:moveCards(move)
    local num = {}
    for i = 0, #ids, 1 do
      table.insert(num, tostring(i))
    end

    local n = tonumber(room:askToChoice(player, {
      choices = num,
      skill_name = skel.name,
      prompt = "#lb__lizhu-ask:::" .. (player:getSwitchSkillState(skel.name) == fk.SwitchYang and "Top" or "Bottom"),
    }))
    if n > 0 then
      local cards = player:drawCards(n, skel.name,
        player:getSwitchSkillState(skel.name) == fk.SwitchYang and "top" or "bottom")
      player:showCards(cards)
      local x = #effect.cards - n
      if x < 1 then
        --room:setPlayerMark(player,"lb__lizhu-turn",1)
        room:invalidateSkill(player, skel.name, "-turn")
      else
        for i = 1, x, 1 do
          local card = Fk:getCardById(cards[i])
          if not player.dead and not table.contains(player:getTableMark("@$lb__lizhu-turn"), card.trueName) and
              (card.type == Card.TypeBasic or
                (card.type == Card.TypeTrick and card.sub_type ~= Card.SubtypeDelayedTrick)) --即时牌
          then
            room:askToUseVirtualCard(player, {
              name = card.name,
              skill_name = skel.name,
              cancelable = false,
            })
            room:addTableMark(player, "@$lb__lizhu-turn", card.trueName)
          end
        end
      end
    end
  end,
})

return skel
